package com.futures.investm.common;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.WindowManager;

import com.futures.investm.MainActivity;
import com.futures.investm.R;
import com.futures.investm.utils.CacheData;
import com.aries.ui.util.StatusBarUtil;
import com.aries.ui.view.title.TitleBarView;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.LogInCallback;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import tech.com.commoncore.base.BaseTitleActivity;
import tech.com.commoncore.utils.DataUtils;
import tech.com.commoncore.utils.FastUtil;
import tech.com.commoncore.utils.ToastUtil;

public class GuideActivity extends BaseTitleActivity {

    @BindView(R.id.viewPager)
    ViewPager mViewPager;

    //引导图片集
    private final int[] guideImgs = new int[]{R.mipmap.one, R.mipmap.two, R.mipmap.three};

    private PagerAdapter mAdapter;

    private ArrayList<Fragment> fragments = new ArrayList<>();

    @Override
    public int getContentLayout() {
        return R.layout.activity_guide;
    }

    @Override
    public void initView(Bundle savedInstanceState) {

        if (!StatusBarUtil.isSupportStatusBarFontChange()) {
            //隐藏状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }

        for (int i = 0; i < 3; i++) {
            fragments.add(GuideFragment.newInstance(guideImgs[i]));
        }

        mAdapter = new PagerAdapter(getSupportFragmentManager(), fragments);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOffscreenPageLimit(3);
    }


    @OnClick(R.id.tv_jump)
    public void onViewClicked() {
        CacheData.setIsFirst(mContext, false);
        if (CacheData.isLogin(mContext)) {
            autoLogin();
        } else {
//            FastUtil.startActivity(mContext, LoginRegistActivity.class);

            if (!CacheData.isLogin(mContext)) {
                FastUtil.startActivity(mContext, MainActivity.class);
                GuideActivity.this.finish();
            }

//            FastUtil.startActivity(mContext, LoginActivity.class);
//            GuideActivity.this.finish();
        }
    }

    //自动登录
    private void autoLogin() {
        //获取缓存账户密码
        String account = CacheData.getLoginAccount(mContext);
        String password = CacheData.getLoginPassword(mContext);
        if (DataUtils.isEmptyString(account) || DataUtils.isEmptyString(password)) {
            //如果账户或密码为空
            CacheData.setLoginUserId(mContext, "");
            CacheData.setLoginUser(null);
            CacheData.setLoginAccount(mContext, "");
            CacheData.setLoginPassword(mContext, "");
//            FastUtil.startActivity(mContext, LoginRegistActivity.class);
            FastUtil.startActivity(mContext, LoginActivity.class);
            GuideActivity.this.finish();
        } else {
            login(account, password);
        }
    }

    //登录
    private void login(final String account, final String password) {
        AVUser.loginByMobilePhoneNumberInBackground(account, password, new LogInCallback<AVUser>() {
            @Override
            public void done(AVUser avUser, AVException e) {
                if (null == e) {
                    //登录成功
                    //缓存登录信息
                    CacheData.setLoginUserId(mContext, avUser.getObjectId());
                    CacheData.setLoginUser(avUser);
                    CacheData.initLoginAccount(mContext, account, password);
                    FastUtil.startActivity(mContext, MainActivity.class);
                    GuideActivity.this.finish();
                } else {
                    //登录失败
                    ToastUtil.show("登录失败,请重新登录");
                    CacheData.setLoginUserId(mContext, "");
                    CacheData.setLoginUser(null);
                    CacheData.setLoginPassword(mContext, "");
//                    FastUtil.startActivity(mContext, LoginRegistActivity.class);
                    FastUtil.startActivity(mContext, LoginActivity.class);
                    GuideActivity.this.finish();
                }
            }
        });
    }


    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setStatusBarLightMode(true)
                .setVisibility(View.GONE);
    }
}
